package com.yaj.mesif.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yaj.mesif.dto.ToOrderDto;
import com.yaj.mesif.dto.U8ToOrderDto;
import com.yaj.mesif.entity.ToOrder;
import com.yaj.mesif.service.IFO1005Service;
import com.yaj.mesif.service.ToOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class IFO1005ServiceImpl implements IFO1005Service {
    @Resource
    private ToOrderService toOrderService;

    @Value("${u8.caccid}")
    private String CACCID;

    @Value("${u8.moautoidcol}")
    private String MOAUTOIDCOL;

    @Value("${u8.cwhcode}")
    private String CWHCODE;

    @Value("${u8.cdepcode}")
    private String CDEPCODE;

    @Value("${u8.url_u8_rdrecord10save}")
    private String url_u8_rdrecord10save;

    private static final String CMAKER = "IFO1005";

    @Override
    public void ifo1005() {
        try {
            List<ToOrderDto> toOrderDtos = toOrderService.ifo1005SelectToOrder();
            log.info("ifo1005 toOrderDtos size:"+ toOrderDtos.size());
            for (ToOrderDto toOrderDto:toOrderDtos
                 ) {
                try {
                    //设置配置文件中的固定值
                    toOrderDto.setCACCID(CACCID);
                    //toOrderDto.setMOAUTOIDCOL(MOAUTOIDCOL);
                    toOrderDto.setCWHCODE(CWHCODE);
                    toOrderDto.setCDEPCODE(CDEPCODE);

                    U8ToOrderDto u8ToOrderDto = new U8ToOrderDto();
                    BeanUtil.copyProperties(toOrderDto,u8ToOrderDto);

                    //检查参数
                    if(checkParameter(u8ToOrderDto)){
                        JSONObject json = JSONUtil.parseObj(u8ToOrderDto, false);
                        json.setDateFormat("yyyy-MM-dd");

                        log.info("ifo1005 u8ToOrderDto paramater:"+ json.toString());

                        String resp = HttpRequest.post(url_u8_rdrecord10save)
                                .body(json.toString())
                                .timeout(20000)
                                .execute().body();

                        log.info("ifo1005 request result:"+ resp);
                        JSONObject jsonObject = new JSONObject(resp);
                        //取得返回结果
                        String result = jsonObject.getStr("Result");
                        //失败原因
                        String description = jsonObject.getStr("Description");
                        //生成的U8单据号
                        String receiptNo = jsonObject.getStr("ReceiptNo");
                        //返回结果为True时
                        if (result.equals("T")) {
                            log.info("Finish Order:{} Success", u8ToOrderDto.getCVOUCHCODE());
                            toOrderDto.setUPDATEDBY("IFO1005");
                            toOrderDto.setUPDATETIMESTAMP(DateTime.now());
                            //更新ToCanOrder中的Process
                            toOrderDto.setPROCESSFLAG("8");

                            toOrderService.ifo1005UpdateToOrder(toOrderDto);
                        }else{
                            log.error("Finish Order:{} Fail", u8ToOrderDto.getCVOUCHCODE());
                            toOrderDto.setUPDATEDBY("IFO1005");
                            toOrderDto.setUPDATETIMESTAMP(DateTime.now());
                            //更新ToCanOrder中的Process
                            toOrderDto.setPROCESSFLAG("9");

                            toOrderService.ifo1005UpdateToOrder(toOrderDto);
                        }
                    }
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            }
        }catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    private boolean checkParameter(U8ToOrderDto u8ToOrderDto){
        if(ObjectUtil.isEmpty(u8ToOrderDto.getCVOUCHCODE())){
            log.error("生产订单不能为空" + u8ToOrderDto.toString());
            return false;
        }
        if(ObjectUtil.isEmpty(u8ToOrderDto.getDDATE())){
            log.error("完成日期不能为空" + u8ToOrderDto.toString());
            return false;
        }
        if(ObjectUtil.isEmpty(u8ToOrderDto.getCMAKER())){
            log.error("制单人不能为空" + u8ToOrderDto.toString());
            return false;
        }
        if(ObjectUtil.isEmpty(u8ToOrderDto.getCINVCODE())){
            log.error("制品编号不能为空" + u8ToOrderDto.toString());
            return false;
        }
        if(ObjectUtil.isEmpty(u8ToOrderDto.getIQUANTITY_JH())){
            log.error("预计生产数量不能为空" + u8ToOrderDto.toString());
            return false;
        }
        if(ObjectUtil.isEmpty(u8ToOrderDto.getIQUANTITY_HG())){
            log.error("合格数量不能为空" + u8ToOrderDto.toString());
            return false;
        }
        if(ObjectUtil.isEmpty(u8ToOrderDto.getIQUANTITY_RET())){
            log.error("不合格数量不能为空" + u8ToOrderDto.toString());
            return false;
        }
        if(ObjectUtil.isEmpty(u8ToOrderDto.getCBATCH())){
            log.error("批号不能为空" + u8ToOrderDto.toString());
            return false;
        }
        if(ObjectUtil.isEmpty(u8ToOrderDto.getAUTOID_MO())){
            log.error("关联明细ID不能为空" + u8ToOrderDto.toString());
            return false;
        }
        return true;
    }
}
